package com.hwj.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hwj.entity.Role;
import com.hwj.entity.User;
import com.hwj.mapper.RoleMapper;
import com.hwj.mapper.UserInfoMapper;
import com.hwj.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userinfoService")
public class UserInfoServiceImpl implements UserInfoService{

    @Autowired
    UserInfoMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    public User doLogin(User user) {
        return userMapper.doLogin(user);
    }

    /*查询指定用户*/
    @Override
    public User queryById(Long id) {
        return userMapper.queryById(id);
    }

    /*登陆*/
    @Override
    public User queryByName(String username) {
        return userMapper.queryByName(username);
    }

    /*注册*/
    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public PageInfo<User> queryByNamePhoneEmail(User user,Integer pageNum,Integer pageSize) {
        String username= user.getUsername();
        String phone=user.getPhone();
        String email=user.getEmail();
        if("".equals(username)){
            username=null;
        }
        if("".equals(phone)){
            phone=null;
        }
        if("".equals(email)) {
            email = null;
        }
        if(username!=null){
            username="%"+username+"%";
        }
        if(phone!=null){
            phone="%"+phone+"%";
        }
        if(email!=null) {
            email = "%" + email + "%";
        }
        if(pageNum==null||pageNum <= 0){  //若为负数
            pageNum = 1;   //设置默认值为1
        }
        if(pageSize == null){ //若页面大小为空
            pageSize = 5;    //设置默认每页显示的数据数
        }
        user.setUsername(username);
        user.setPhone(phone);
        user.setEmail(email);
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.queryByNamePhoneEmail(user);
        return new PageInfo<>(users);
    }

    /*查询*/

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<User> getExcellentUser() {
        return userMapper.getExcellentUser();
    }


    /*加载用户信息,用于spring security身份验证*/
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        /*查询用户信息*/
        User user=userMapper.queryByName(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        /*查询用户信息*/
        Role role = roleMapper.selectByPrimaryKey(user.getRoleId());

        String password=user.getPassword();
        String roleName="ROLE_"+role.getRoleName();
        /*security存储*/
        return new org.springframework.security.core.userdetails
                .User(username,password,AuthorityUtils
                .commaSeparatedStringToAuthorityList(roleName));
    }
}
